package com.lxq.live.util;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class LiveGoApi {
    public void generateLiveRoomKey(String randomSalt) {

    }

    public String syncRequest(String url, String method, String param) {
        HttpURLConnection connection;
        var stringBuilder = new StringBuilder();
        try {
            var urlObj = new URL(url);
            connection = (HttpURLConnection) urlObj.openConnection();
            connection.setRequestMethod(method);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.getOutputStream().write(param.getBytes());
            if (connection.getResponseCode() == 200) {

                var reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                var lineStr = "";
                while ((lineStr = reader.readLine()) != null) {
                    stringBuilder.append(lineStr);
                }
                return stringBuilder.toString();
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
